//	Pixlib
import com.bourre.events.IEvent;
import com.bourre.events.IEventDispatcher;
import com.bourre.log.Logger;
import com.bourre.log.LogLevel;

//	Oregano
import org.omus.core.Group;

//	Pixlib API for Oregano
import net.webbymx.pixgano.handlers.group.GroupHandlerManager;
import net.webbymx.pixgano.handlers.data.PropertySetHandlerManager;


class net.webbymx.pixgano.handlers.group.AGroupHandler 
	implements net.webbymx.pixgano.handlers.group.IGroupHandler 
{
/* ****************************************************************************
* PRIVATE VARIABLES
**************************************************************************** */	
	private var _oEB	: IEventDispatcher;
	private var _sCfgID	:String;				//	???
	private var _sGrp	:String;				// the groupID this handler act on


/* ****************************************************************************
* CONSTRUCTOR
**************************************************************************** */
	function AGroupHandler() {
		_sCfgID = Group.getInstance().getConfigID();	
		_oEB = GroupHandlerManager.getInstance().getBroadcaster();
	}
	

/* ****************************************************************************
* PRIVATE FUNCTIONS
**************************************************************************** */
	private function _fireEvent( e:IEvent ) : Void 
	{
		_oEB.broadcastEvent( e );
	}


/* ****************************************************************************
* PUBLIC FUNCTIONS
**************************************************************************** */
	public function onClose() : Void {}
	
	public function onChange() : Void 
	{
		// change log to FATAL to make it easy to retrieve. Just for debug purpose
//Logger.LOG( "AGroupHandler :: ID:" + Group.getInstance().getConfigID()+" name:"+Group.getInstance().getName() + " :: onChange", LogLevel.INFO );
		// Thomas :: Is this event broadcasted automatically at the first group creation ?
		// Xavier :: No. That's why if you are looking in the ClientLogin line 47 to 49, we need to initialize it.
		// what we could do is create a LoginHandler and call the function with the LoginHandler as parameter in the _init of the application.
		// TODO create the LoginHandler and switchGroup at application initialization or find a better way to do it.
		// Maybe when creating the GroupHandlerManager, automatically swithching the group to the Login one ( no call in the Application.as class )
//Logger.LOG("AGroupHandler :: old:" + _sCfgID + "new : " + Group.getInstance().getConfigID(), LogLevel.INFO );

	//	if the group id is different
		if( _sCfgID != Group.getInstance().getConfigID() ) {
			_sCfgID = Group.getInstance().getConfigID();
			
		//	change the group handler
			GroupHandlerManager.getInstance().switchGroupHandler( _sCfgID );
			
		//	register the PropertySetHandler
			PropertySetHandlerManager.getInstance().createPropertySetHandler( _sCfgID, Group.getInstance().getProperties() );
		}
		
		//	create here the PropertySetHandler using the PropertySet of the Group.
	}
	
	public function onChangeInit() : Void {}
	
	public function onError() : Void {}
	
	public function onUserJoined() : Void {}
	
	public function onUserLeft() : Void {}
	
	public function onOpen() : Void {}
}
